package ee.metadataUtils;

import java.util.HashMap;
import java.util.Map;

//TODO check if it's still a good plan to add the final keyword where you can (applies to all projects), it probably is a common misconception 
public class ClassUtils
{
	private static final Map<Class<?>, Class<?>> WRAPPER_TYPES = getWrapperTypes();
	
	private static Map<Class<?>, Class<?>> getWrapperTypes()
    {
		Map<Class<?>, Class<?>> wrapperTypes = new HashMap<Class<?>, Class<?>>();
        wrapperTypes.put(Boolean.TYPE, Boolean.class);
        wrapperTypes.put(Character.TYPE, Character.class);
        wrapperTypes.put(Byte.TYPE, Byte.class);
        wrapperTypes.put(Short.TYPE, Short.class);
        wrapperTypes.put(Integer.TYPE, Integer.class);
        wrapperTypes.put(Long.TYPE, Long.class);
        wrapperTypes.put(Float.TYPE, Float.class);
        wrapperTypes.put(Double.TYPE, Double.class);
        wrapperTypes.put(Void.TYPE, Void.class);
        
        return wrapperTypes;
    }
	
	static public Class<?> getWrapper(Class<?> primitiveType)
	{
		return WRAPPER_TYPES.get(primitiveType);
	}
	
	static public boolean isWrapper(Class<?> wrapperType)
	{
		return WRAPPER_TYPES.containsValue(wrapperType);
	}
	
	static public boolean isPrimitive(Class<?> type)
	{
		return type.isPrimitive();
	}
	
	static public Object getDefaultValue(Class<?> type)
	{
		if (type.equals(Byte.TYPE))
		{
			return 0;
		} else if (type.equals(Short.TYPE))
		{
			return 0;
		} else if (type.equals(Integer.TYPE))
		{
			return 0;
		} else if (type.equals(Long.TYPE))
		{
			return 0L;
		} else if (type.equals(Float.TYPE))
		{
			return 0.0f;
		} else if (type.equals(Double.TYPE))
		{
			return 0.0d;
		} else if (type.equals(Character.TYPE))
		{
			return '\u0000';
		} else if (type.equals(Boolean.TYPE))
		{
			return false;
		} else
		{
			return null;
		}
	}

}
